package com.sq.auth.application.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sq.auth.application.dto.AuthPermissionDTO;
import com.sq.auth.application.dto.AuthPermissionDTO;
import com.sq.auth.common.entity.Result;
import com.sq.auth.domain.bo.AuthPermissionBO;
import com.sq.auth.domain.bo.AuthPermissionBO;
import com.sq.auth.domain.service.AuthPermissionDomainService;
import com.sq.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Permission;
import java.util.List;

/**
 * @author quan
 */
@RestController
@RequestMapping("/permission/")
@Slf4j
public class AuthPermissionController {
    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    @PostMapping("add")
    public Result add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
        }
        try {
            Preconditions.checkArgument(StrUtil.isNotBlank(authPermissionDTO.getName()), "权限名称不能为空");
            Preconditions.checkArgument(StrUtil.isNotBlank(authPermissionDTO.getPermissionKey()), "权限标识不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "权限父级id不能为空");
            AuthPermissionBO authPermissionBO =   BeanUtil.copyProperties(authPermissionDTO, AuthPermissionBO.class);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.error("AuthPermissionController.add.error:{}", e.getMessage());
            return Result.fail("新增权限失败");
        }
    }

    @PostMapping("update")
    public Result update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
        }
        try {
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO =  BeanUtil.copyProperties(authPermissionDTO, AuthPermissionBO.class);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            log.error("AuthPermissionController.update.error:{}", e.getMessage());
            return Result.fail("更新权限失败");
        }
    }

    @PostMapping("delete")
    public Result delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
        }
        try {
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO =  BeanUtil.copyProperties(authPermissionDTO, AuthPermissionBO.class);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            log.error("AuthPermissionController.delete.error:{}", e.getMessage());
            return Result.fail("删除权限失败");
        }
    }

    @GetMapping("getPermission")
    public Result<List<String>> getPermission(@RequestParam("userName") String userName) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionController.getPermission.userName:{}", JSON.toJSONString(userName));
        }
        try {
            Preconditions.checkArgument(StrUtil.isNotBlank(userName),"用户名不能为空");
            return Result.ok(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("AuthPermissionController.getPermission.error:{}", e.getMessage());
            return Result.fail("获取用户权限失败");
        }
    }
}
